<?php
function get_page($psize, $pindex = 1, &$totalp, $tablename, $where = "",$orderby = "")
{
    if ($where) $where = 'where '.$where;
    if ($orderby) $orderby = 'order by '.$orderby;
    $q_count_total = "select count(*) from ". $tablename." ".$where; //thuc hien dem so dong
    $rs = mysql_query($q_count_total); //thuc thi xong cau truy van
    if ($rs)
    {
        $rs_c = mysql_fetch_row($rs);
        mysql_free_result($rs);
        $numrows = $rs_c[0];
    }
    if ($numrows <= 0)
    {
        $pindex = 0;
         return;
    }
    if ($psize > 0)
        $lastpage = ceil($numrows/$psize);
    else
        $lastpage = 1;
    if ($pindex == '' || $pindex <= '1') {
        $pindex = 1;
    }
    elseif ($pindex > $lastpage) {
        $pindex = $lastpage;
    }
    if ($psize > 0) {
        $limit_str = 'limit ' .($pindex - 1) * $psize .',' .$psize;
    }
    else { $limit_str = NULL; }
    $query = "select * from $tablename $where $orderby $limit_str";
    $result = mysql_query($query);
   	$totalp = $lastpage ;

    return $result;
}
function print_page($totalpage, $pcurent)
{
	$begin = $pcurent - 3;
	$end = $pcurent + 3;
	if ($begin <= 0) $begin = 1;
	if ($end > $totalpage) $end = $totalpage;
	$str = '';
	for($i = $begin; $i <= $end; $i++)
	{
		if ($i != $pcurent)
			$str .= "<a href=\"?p=$i\">$i</a> | ";
		else
			$str .= "[$i] | ";
	}
	return trim($str, " | ");
}
 
?>